package com.sibu.yx.promotion.web.support.config;

import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Filter;

import com.sibu.yx.promotion.service.config.JwtTokenProperties;
import com.sibu.yx.promotion.service.support.util.JwtTokenUtil;
import com.sibu.yx.promotion.web.support.filter.CustomWebSessionManage;
import com.sibu.yx.promotion.web.support.filter.HeadTokenAuthenticationFilter;
import com.sibu.yx.promotion.web.support.model.CustomPermissionResolver;
import com.sibu.yx.promotion.web.support.model.MyShiroRealm;
import com.sibu.yx.promotion.web.support.model.TokenShiroRealm;

import org.apache.shiro.authc.credential.AllowAllCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilter;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @author     ：yuwenbo
 * @date       ：Created in 2019/4/11 17:45
 * @description：shiro配置中心
 */
@Configuration
public class ShiroConfig {


    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/", "anon");
        filterChainDefinitionMap.put("/**", "authc");
        shiroFilterFactoryBean.setFilters(createCustomFilerMap());
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }


    /**
     * 创建本地的拦截器
     * @return
     */
    private Map<String,Filter> createCustomFilerMap(){
        Map<String,Filter> filterMap=new HashMap<>();
        //DefaultFilter ,替换默认的拦截器
        filterMap.put(DefaultFilter.authc.name(),new HeadTokenAuthenticationFilter());
        return filterMap;
    }
    @Bean
    public SecurityManager securityManager(){
        DefaultWebSecurityManager securityManager =  new DefaultWebSecurityManager();
        //securityManager.setSessionManager(customWebSessionManage());
        securityManager.setRealms( Arrays.asList(tokenShiroRealm()));
        securityManager.setRememberMeManager(null);
        return securityManager;
    }

    /**
     * 本地缓存管理
     * @return
     */
    public CustomWebSessionManage customWebSessionManage(){
        CustomWebSessionManage customWebSessionManage=new CustomWebSessionManage();
        customWebSessionManage.setSessionIdCookieEnabled(true);
        Cookie cookie=new SimpleCookie();
        cookie.setName("web.uid");
        cookie.setDomain("/");
        cookie.setMaxAge(3600*4*1000);//
        customWebSessionManage.setSessionIdCookie(cookie);
        return customWebSessionManage;
    }


    public MyShiroRealm shiroRealm(){
        MyShiroRealm shiroRealm = new MyShiroRealm();
        return shiroRealm;
    }


    public TokenShiroRealm tokenShiroRealm(){
        TokenShiroRealm tokenShiroRealm=new TokenShiroRealm();
        tokenShiroRealm.setPermissionResolver(new CustomPermissionResolver());
        tokenShiroRealm.setCredentialsMatcher(new AllowAllCredentialsMatcher());
        return tokenShiroRealm;
    }

    @Bean
    public JwtTokenUtil createJwtTokenUtil(JwtTokenProperties jwtTokenProperties){
        return new JwtTokenUtil(jwtTokenProperties);
    }



}
